<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:replace="fragment/head"/>
    <!-- BEGIN PAGE LEVEL PLUGINS -->
    <link href="resources/assets/global/plugins/datatables/datatables.min.css" rel="stylesheet" type="text/css"/>
    <link href="resources/assets/global/plugins/datatables/plugins/bootstrap/datatables.bootstrap.css" rel="stylesheet"
          type="text/css"/>
    <link href="resources/assets/global/plugins/jstree/dist/themes/default/style.min.css" rel="stylesheet"
          type="text/css"/>
    <!-- END PAGE LEVEL PLUGINS -->
</head>

<body class="page-header-fixed page-sidebar-closed-hide-logo page-container-bg-solid page-content-white page-sidebar-closed">
<th:block th:with="
    ERPConstants = ${T(com.sh.caryr.erp.util.ERPConstants)} ,
    authTarget = ${ERPConstants.AUTH_TARGET_USERROLE} ,
    layout = ${T(com.sh.caryr.erp.custmz.LayoutContainer).getLayout(authTarget, session.user)},
    createAuth = ${authTarget + '_' + ERPConstants.AUTH_ACTVT_CREATE},
    deleteAuth = ${authTarget + '_' + ERPConstants.AUTH_ACTVT_DELETE},
    updateAuth = ${authTarget + '_' + ERPConstants.AUTH_ACTVT_UPDATE}
">
    <!-- BEGIN HEADER INNER -->
    <th:block th:replace="fragment/header"/>
    <!-- BEGIN CONTAINER -->
    <div class="page-container">
        <!-- BEGIN SIDEBAR -->
        <th:block th:replace="fragment/sidebar"/>
        <!-- BEGIN CONTENT -->
        <div class="page-content-wrapper">
            <!-- BEGIN CONTENT BODY -->
            <div class="page-content">
                <!-- BEGIN PAGE BAR -->
                <div class="page-bar">
                    <ul class="page-breadcrumb">
                        <li><a href="index.html" class='msg_home'>主页</a> <i class="fa fa-circle"></i></li>
                        <li><span class='msg_userRole'>管理</span></li>
                    </ul>
                </div>
                <!-- BEGIN PAGE TITLE-->
                <h1 class="page-title">
                    <span class="msg_title_userRole">   PageTitle</span>
                    <small class="msg_subtitle_userRole">sub title</small>
                </h1>
                <!-- END PAGE TITLE-->
                <!-- BEGIN main content -->
                <div class="row">
                    <!-- BEGIN ===========page level content============= -->
                    <div class="col-md-12">
                        <!-- BEGIN data table-->
                        <div class="portlet box green datatable-portlet">
                            <div class="portlet-title">
                                <div class="caption">
                                    <i class="fa fa-list"></i><span class='msg_listTitle'>列表</span>
                                </div>
                                <div class="tools">
                                    <th:block th:if="${ session.isSuperUser||session.authStr.contains(createAuth)}">
                                        <button id="btn_add_entity" class="btn sbold green">
                                            <i class="fa fa-plus"></i>
                                        </button>
                                        <button id="btn_copy_entityRole" class="btn sbold green">
                                            <i class="fa fa-files-o"></i>
                                        </button>
                                    </th:block>
                                    <button th:if="${ session.isSuperUser||session.authStr.contains(deleteAuth)}"
                                            id="btn_del_entity" class="btn sbold green">
                                        <i class="fa icon-trash"></i>
                                    </button>
                                    <button id="btn_refresh_dt" class="btn sbold green">
                                        <i class="fa icon-refresh"></i>
                                    </button>
                                </div>
                            </div>
                            <div class="portlet-body">
                                <table class="table table-striped table-bordered table-hover dt-responsive"
                                       id="dt_userRole" cellspacing="0" width="100%">
                                    <thead>
                                    <tr>
                                        <th width="2%"><label
                                                class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
                                            <input type="checkbox" class="group-checkable"
                                                   data-set="#dt_userRole .checkboxes"/> <span></span>
                                        </label></th>
                                        <th width='' class='msg_ent_fld_name'></th>
                                        <th width='' class='msg_ent_fld_description'></th>
                                        <th width='10%' class='msg_ent_fld_updateAt'></th>
                                        <th width='10%' class='msg_ent_fld_createAt'></th>
                                        <th width="2%"></th>
                                    </tr>
                                    </thead>
                                </table>
                            </div>
                        </div>
                        <!-- END data table-->
                    </div>

                    <!-- BEGIN detail modal form -->
                    <div id="detailModal" class="formModal modal fade" tabindex="-1"
                         role="basic" aria-hidden="true" style="display: none;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-hidden="true"></button>
                                    <h4 class="modal-title">角色</h4>
                                </div>
                                <div class="modal-body">
                                    <!-- BEGIN Error Message -->
                                    <div class="alert alert-danger hidden" id="form_error_msg">
                                        <button type="button" class="close" data-dismiss="alert"
                                                aria-hidden="true"></button>
                                        <strong class="msg_err_title"></strong>
                                        <span class="msg_err_subTitle"> </span>
                                    </div>
                                    <form role="form" id='userRole_edit_form'>
                                        <div class="form-body">
                                            <div class='row edit_data'>
                                                <input class='hidden' type='text' name='id'/>
                                                <input class='hidden' type='text' name='coco' value="FCOR"/>
                                                <div class='form-group col-md-6 col-sm-12 col-xs-12 has-success '>
                                                    <label class='col-md-12 control-label msg_ent_fld_name'></label>
                                                    <div class='col-md-12'>
                                                        <div class='input-group'>
                                                            <input type='text' class='form-control' name='name'
                                                                   maxlength="50" id='input_edit_form_name'
                                                                   placeholder=''>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class='form-group col-md-6 col-sm-12 col-xs-12 has-success '>
                                                    <label class='col-md-12 control-label msg_ent_fld_shop'></label>
                                                    <div class='col-md-12'>
                                                        <div class='input-group'>
                                                            <select class="form-control sel_shop"
                                                                    name="shopId"></select>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class='form-group col-md-12 col-sm-12 col-xs-12 has-success '>
                                                    <label class='col-md-12 control-label msg_ent_fld_description'></label>
                                                    <div class='col-md-12'>
                                                        <div class='input-group'>
                                                        <textarea class='form-control' name='description'
                                                                  maxlength="255"></textarea>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                    <div class="portlet box ">
                                        <div class="portlet-body">
                                            <div class="row">
                                                <div class="col-md-3 col-sm-3 col-xs-3">
                                                    <ul class="nav nav-tabs tabs-left" id="nav_auth_tree">
                                                    </ul>
                                                </div>
                                                <div class="col-md-9 col-sm-9 col-xs-9">
                                                    <div class="tab-content">
                                                        <div class="tab-pane active in" id="tab_auth_tree">
                                                            <div id="auth_tree" class="tree-demo"></div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn green msg_com_formSave" id="btn_edit_form_save">保存
                                    </button>
                                    <button type="button" data-dismiss="modal"
                                            class="btn grey-salsa btn-outline msg_com_formReturn"
                                            id="btn_edit_form_return">返回
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- END   ===========page level content============= -->
                </div>
            </div>
        </div>
    </div>
    <th:block th:replace="fragment/rear"/>
    <!-- BEGIN PAGE LEVEL PLUGINS -->
    <script src="resources/assets/global/plugins/datatables/datatables.min.js"></script>
    <script src="resources/assets/global/plugins/datatables/plugins/bootstrap/datatables.bootstrap.js"></script>
    <script src="resources/assets/global/plugins/bootstrap-maxlength/bootstrap-maxlength.min.js"></script>
    <script src="resources/assets/global/plugins/jstree/dist/jstree.min.js"></script>
    <script src="resources/assets/caryr/caryr.datatable.js" type="text/javascript"></script>
    <script src="resources/assets/caryr/caryr.auth.js"></script>
    <!-- BEGIN PAGE LEVEL SCRIPTS -->
    <script th:inline="javascript">
        jQuery(document).ready(function () {

            /*[+
            caryr.lang = [[${session.lang}]];
            var disableModify = ('true'==[[${session.isSuperUser||session.authStr.contains(updateAuth)?'false':'true'}]]);
            var const_authValue_all = [[${ERPConstants.AUTH_VALUE_ALL}]];
            +]*/

            var tableId = '#dt_userRole',
                formId = '#userRole_edit_form';

            /************ Get Reference object list***/
            function fatchRef() {
                caryr.fetchRefList({
                    replace: true,
                    refSelect: [$('.sel_shop')],
                    descriptionField: 'shopName',
                    url: 'api/shop'
                });
            }

            fatchRef();
            /***************** dataTable **************/
            var dataTable = caryr.dataTable.create({
                url: "api/userRole",
                defaultOrderBy: 1,
                tableId: tableId,
                lang: caryr.lang,
                columns: [
                    {data: 'name'},
                    {data: 'description'},
                    {data: 'updateAt'},
                    {data: 'createAt'}
                ],
                onViewEvent: function (data) {
                    caryr.refreshEditForm(data, false, formId); //Refresh form
                    updateTreeDataByAuths(authTree, data.authList);   //Refresh tree
                    $('#detailModal').modal('show');
                }
            });

            $('#btn_add_entity').on('click', function () {	//attach create entity
                caryr.refreshEditForm({}, true, formId);
                $('#detailModal').modal('show');
            });
            /***************** Edit form **************/
            caryr.getFormInstatnce('userRole_edit_form', {
                disabled: disableModify,
                url: 'api/userRole',
                validateForm: function (formResult, form) {
                    if (!formResult) {
                        $('#detailModal').modal('show');
                    } else {
                        $('#detailModal').modal('hide');
                    }
                    return formResult;
                },
                afterCollectEntityData: function (entity) {
                    var auths = collectAuth(authTree);
                    entity.authList = auths;
                },
                afterSaveSuccess: function (data, action) {
                    var id = action == "POST" ? data.data : data.data.id;
                    if (id) {
                        this.form.find('.edit_data input[name="id"]').val(id);
                        dataTable.draw();
                        caryr.showTab('#tab_li_entity_list');
                    } else {
                        swal({
                            title: $.i18n.prop("msg_com_systemError"),
                            text: "",
                            type: "error"
                        });
                    }
                },
                afterSaveAndNewSuccess: function (data, action) {
                    this.form.find('.edit_data input[name="id"]').val("");
                    dataTable.draw();
                }
            });
            /***************** tree **************/
            var authTree = $('#auth_tree');
            var nav_auth_tree = $('#nav_auth_tree');
            var treeData = caryr.auth.treeData;
            initTreeNav(authTree, treeData);

            createTree(treeData, function () {
                var category = nav_auth_tree.find('li.active a').attr('data-category');
                toggleAuthCategory(authTree, category);

                if (disableModify) {
                    var jsonNodes = $('#auth_tree').jstree().get_json('#', {flat: true});
                    $.each(jsonNodes, function (i, val) {
                        $('#auth_tree').jstree().disable_node(val);
                    })
                }
            });

            function collectAuth(tree) {
                var auths = new Array();
                var selectedNodes = tree.jstree("get_selected");
                for (var i = 0; i < selectedNodes.length; i++) {
                    var nodeAuth = selectedNodes[i].split('_');
                    var auth = {};
                    if (nodeAuth.length == 2) {
                        auth.authTarget = nodeAuth[0];
                        auth.authActvt = nodeAuth[1];
                        auth.authField = const_authValue_all;
                        auth.authValue = '';
                        auths.push(auth);
                    } else if (nodeAuth.length == 3) {
                        auth.authTarget = nodeAuth[0];
                        auth.authActvt = nodeAuth[1];
                        auth.authField = nodeAuth[2];
                        auth.authValue = '';
                        auths.push(auth);
                    }
                }
                return auths;
            }

            function updateTreeDataByAuths(tree, auths) {
                tree.jstree("uncheck_all");
                var selectedNodeIds = {};
                $('#auth_tree').jstree().open_all();
                for (var i = 0; i < auths.length; i++) {
                    var auth = auths[i];
                    var nodeId = '';
                    if (auth.authField == const_authValue_all) {
                        nodeId = '#' + auth.authTarget + '_' + auth.authActvt;
                    } else {
                        nodeId = '#' + auth.authTarget + '_' + auth.authActvt + '_' + auth.authField;
                    }
                    selectedNodeIds[nodeId] = true;
                    tree.jstree("check_node", $(nodeId));
                }
                $('#auth_tree').jstree().close_all();
            }

            function initTreeNav(tree, treeData) {
                var nav = $("#nav_auth_tree");

                var temp = '';
                for (var i in treeData) {
                    var category = treeData[i].category;
                    var activeHtml = 'active';
                    if (i != 0) {
                        activeHtml = '';
                    }
                    if (temp != category) {
                        /* var label =$.i18n.prop("msg_"+category); */
                        var label = "msg_" + category;
                        var html = "<li class='" + activeHtml + "'>" +
                            "<a href='#tab_auth_tree' data-toggle='tab' class=' " + label + "'" +
                            "data-category='" + category + "' aria-expanded='false'></a></li>";
                        nav.append(html);
                    }
                    temp = category;
                }
                //Attach click event to toggle tree nodes
                nav.find('li').on('click', function () {
                    var category = $(this).find('a').attr('data-category');
                    toggleAuthCategory(tree, category);
                });
            }

            function toggleAuthCategory(tree, category) {
                //hidden all lv 1 tree node
                tree.find('ul > li[aria-level="1"]').addClass('hidden');
                var id = 'i.category_' + category;
                var s = $(id).closest('li[role="treeitem"').removeClass('hidden');
            }

            function createTree(trData, cb) {
                authTree.bind('loaded.jstree', cb);
                authTree.jstree({
                    'plugins': ["wholerow", "checkbox", "types"],
                    'core': {
                        "themes": {
                            "responsive": false
                        },
                        'data': trData
                    },
                    "types": {
                        "default": {
                            "icon": "fa fa-folder icon-state-warning icon-lg"
                        },
                        "file": {
                            "icon": "fa fa-file icon-state-warning icon-lg"
                        }
                    }
                })
            }
        });
    </script>
</th:block>
</body>

</html>